package com.anbang.app.ui.animation.views

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import com.anbang.app.R

/**
 * @Description:
 * @Author: AnBang
 * @Date: 2019/1/21 14:47
 */
class DiscrollViewContent(context: Context, attrs: AttributeSet) : LinearLayout(context, attrs) {

    init {
        orientation = LinearLayout.VERTICAL
    }

    override fun generateLayoutParams(attrs: AttributeSet): LinearLayout.LayoutParams {
        // 得到xml里面穿过来的参数
        return MyLayoutParams(context, attrs)
    }

    override fun addView(child: View?, index: Int, params: ViewGroup.LayoutParams?) {
        //从child里面拿到我自定义的属性，传到discrollvableView里面
        val p = params as MyLayoutParams
        if (!isDiscrollvable(p)) {//判断该view是否穿了自定义属性值，不是就不需要执行动画，不包一层FrameLayout
            super.addView(child, index, params)
        } else {
            //在addView里面插一脚，往child外面包裹一层FrameLayout
            val discrollvableView = DiscrollvableView(context)
            discrollvableView.setmDiscrollveAlpha(p.mDiscrollveAlpha)
            discrollvableView.setmDisCrollveTranslation(p.mDisCrollveTranslation)
            discrollvableView.setmDiscrollveScaleX(p.mDiscrollveScaleX)
            discrollvableView.setmDiscrollveScaleY(p.mDiscrollveScaleY)
            discrollvableView.setmDiscrollveFromBgColor(p.mDiscrollveFromBgColor)
            discrollvableView.setmDiscrollveToBgColor(p.mDiscrollveToBgColor)

            discrollvableView.addView(child)
            super.addView(discrollvableView, index, params)
        }

    }


    private fun isDiscrollvable(p: MyLayoutParams): Boolean {
        return p.mDiscrollveAlpha ||
                p.mDiscrollveScaleX ||
                p.mDiscrollveScaleY ||
                p.mDisCrollveTranslation != -1 ||
                p.mDiscrollveFromBgColor != -1 && p.mDiscrollveToBgColor != -1
    }


    class MyLayoutParams(context: Context, attrs: AttributeSet) : LayoutParams(context, attrs) {
        var mDiscrollveFromBgColor: Int = 0//背景颜色变化开始值
        var mDiscrollveToBgColor: Int = 0//背景颜色变化结束值
        var mDiscrollveAlpha: Boolean = false//是否需要透明度动画
        var mDisCrollveTranslation: Int = 0//平移值
        var mDiscrollveScaleX: Boolean = false//是否需要x轴方向缩放
        var mDiscrollveScaleY: Boolean = false//是否需要y轴方向缩放

        init {
            // 从child里面拿到我自定义的属性
            val a = context.obtainStyledAttributes(attrs, R.styleable.DiscrollView_LayoutParams)
            mDiscrollveAlpha = a.getBoolean(R.styleable.DiscrollView_LayoutParams_discrollve_alpha, false)
            mDiscrollveScaleX = a.getBoolean(R.styleable.DiscrollView_LayoutParams_discrollve_scaleX, false)
            mDiscrollveScaleY = a.getBoolean(R.styleable.DiscrollView_LayoutParams_discrollve_scaleY, false)
            mDisCrollveTranslation = a.getInt(R.styleable.DiscrollView_LayoutParams_discrollve_translation, -1)
            mDiscrollveFromBgColor = a.getColor(R.styleable.DiscrollView_LayoutParams_discrollve_fromBgColor, -1)
            mDiscrollveToBgColor = a.getColor(R.styleable.DiscrollView_LayoutParams_discrollve_toBgColor, -1)
            a.recycle()
        }

    }
}